package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import hcn.base.ProductBanner;
import org.atag.dao.util.BaseDaoImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lanyu
 * @Description: 广告管理dao层
 * @Date: create in 2022/8/5 10:24
 */
public abstract class ProductBannerDAO extends BaseDaoImpl<ProductBanner> {
    public ProductBannerDAO() {
        this.setKeyField("id");
    }


    /**
     * 产品广告列表
     * @param tenantPId
     * @param bannerPostion
     * @return
     */

    public List<Map<String, Object>> queryProductBannerInfo(final String tenantPId, final String bannerPostion) {
        Map<String, Object> params = new HashMap<>();

        String whereStr = "";
        if (!"00".equals(bannerPostion))//全部
        {
            whereStr = " and position= :position";
            params.put("position", bannerPostion);
        }
        String sql = "select * from base_productbanner"
                + " where status='1' " + whereStr + " order by tenant_p_id,order_no";

        return super.findListBySql(sql, params, Map.class);
    }

    public List<ProductBanner> queryProductBannerInfo2(final String tenantPId, final String bannerPostion) {
        Map<String, Object> params = new HashMap<>();

        String whereStr = "";
        if (!"00".equals(bannerPostion))//全部
        {
            whereStr = " and position= :position";
            params.put("position", bannerPostion);
        }
        String sql = "select * from base_productbanner"
                + " where status='1' " + whereStr + " order by tenant_p_id,order_no";

        return super.findListBySql(sql, params, ProductBanner.class);
    }

    /**
     * 产品广告列表-按租户产品查询
     *
     * @param tenantPId
     * @return
     */
    @DAOMethod(sql = "from ProductBanner where tenantPId=:tenantPId and status='1' " +
            " order by tenantPId,position,orderNo,lastModify desc")
    public abstract List<ProductBanner> queryProductBannerInfoByTenantPId(@DAOParam("tenantPId") int tenantPId);

    /**
     * 产品广告信息-根据产品广告Id明细信息
     *
     * @param id
     * @return
     */
    @DAOMethod(sql = "from ProductBanner where id=:id and status='1'")
    public abstract ProductBanner getProductBannerDetailById(@DAOParam("id") int id);

    /**
     * 逻辑删除
     * @param id
     */
    @DAOMethod(sql = "update ProductBanner set status='0',lastModify=Now() where id=:id")
    public abstract void delete(@DAOParam("id") int id);

    /**
     * 更改排序
     * @param id
     */
    @DAOMethod(sql = "update ProductBanner set orderNo=1,lastModify=Now() where id=:id")
    public abstract void update(@DAOParam("id") int id);

    /**
     * 更改排序
     * @param tenantPId
     * @param position
     * @param orderNo
     */
    @DAOMethod(sql = "update ProductBanner set orderNo=orderNo+1,lastModify=Now() " +
            "where tenantPId=:tenantPId and position=:position and orderNo<:orderNo ")
    public abstract void updateTop
    (@DAOParam("tenantPId") int tenantPId
            ,@DAOParam("position") String position
            ,@DAOParam("orderNo") int orderNo
    );

    /**
     * 更改排序
     * @param tenantPId
     * @param position
     * @param orderNo
     */
    @DAOMethod(sql = "update ProductBanner set orderNo=orderNo-1,lastModify=Now() " +
            "where tenantPId=:tenantPId and position=:position and orderNo>:orderNo ")
    public abstract void updateDown
    (@DAOParam("tenantPId") int tenantPId
            ,@DAOParam("position") String position
            ,@DAOParam("orderNo") int orderNo
    );

    /**
     * 增加广告点击统计次数
     * @param id
     */
    @DAOMethod(sql = "update ProductBanner set clicks=clicks+1 where id=:id")
    public abstract void updateClick(@DAOParam("id") int id);

    /**
     * 动态获取产品广告
     * @param tenantPCode
     * @param position
     * @return
     */
    public List<Map<String, Object>> queryProductBannerList(final String tenantPCode,final String position) {
        Map<String, Object> params = new HashMap<>();
        String sql = "select b.id,b.tenant_p_id,b.position,b.name,b.description,b.link_type,b.link_address,b.link_text,b.order_no"
                + " ,b.picture from base_tenantproducts a inner join base_productbanner b on a.tenant_p_id = b.tenant_p_id"
                + " where a.tenant_p_code = :tenantPCode  and b.position = :position and b.status='1' "
                + " and NOW() BETWEEN b.valid_start_date and b.valid_end_date and (default_flag is null or default_flag<>'1')"
                + " order by order_no";
        params.put("tenantPCode", tenantPCode);
        params.put("position", position);
        return super.findListBySql(sql, params, Map.class);
    }

    /**
     * 动态获取产品广告(默认)
     * @param tenantPCode
     * @param position
     * @return
     */
    public List<Map<String, Object>> queryProductBannerListDefault(final String tenantPCode,final String position) {
        Map<String, Object> params = new HashMap<>();

        String sql = "select b.tenant_p_id,b.position,b.name,b.description,b.link_type,b.link_address,b.link_text,b.order_no"
                + " ,b.picture from base_tenantproducts a inner join base_productbanner b on a.tenant_p_id = b.tenant_p_id"
                + " where a.tenant_p_code = :tenantPCode and b.position = :position and b.status='1' "
                + " and default_flag = '1' order by order_no";
        params.put("tenantPCode", tenantPCode);
        params.put("position", position);
        return super.findListBySql(sql, params, Map.class);

    }
}
